package com.yuandian.business.monitor.controller;

import com.yuandian.api.business_monitor.po.QueryRetStatisInfoParams;
import com.yuandian.api.business_monitor.po.SegmentOverviewParams;
import com.yuandian.api.business_monitor.vo.RetStatisInfoVo;
import com.yuandian.api.business_monitor.vo.SegmentOverviewRoadVo;
import com.yuandian.api.business_monitor.vo.SegmentOverviewVo;
import com.yuandian.api.business_monitor.vo.SegmentPositionVo;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.business.monitor.service.ISegmentOverviewService;
import com.yuandian.constant.Constants;
import com.yuandian.utils.BeanUtils;
import com.yuandian.utils.PageDataInfo;
import com.yuandian.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 说明：网端总览Controller
 *
 * @Author yanjun
 * @Date 2020/10/12 13:56
 */
@RestController
@Api(value = "segmentOverview", tags = "网端总览接口")
@RequestMapping("/segment/overview")
public class SegmentOverviewController {

    @Autowired
    private ISegmentOverviewService segmentOverviewService;


    /**
     * 查询网端总览路径图
     *
     * @param params 网端总览查询对象
     * @return 通用返回对象
     */
	@ApiOperation(value = "网端监控详情-查询网端总览路径图", notes = "网端监控详情-查询网端总览路径图")
    @PostMapping("/queryOverviewRoad")
    public R queryOverviewRoad(@RequestBody SegmentOverviewParams params) {
        // 调用服务
		List<Map<String, Object>> vo = segmentOverviewService.queryOverviewRoad(params);
        return R.ok(vo);
    }

    /**
     * 查询网端指标趋势
     * 请求量、平均响应时长、失败请求量、告警请求量-busi_sys_segment_history_trend_m-SystemSegmentStatTask-系统网端趋势统计-延迟5分钟-api/modules/segment/overview/queryIndicatorTrend
     * @param params 网端总览查询对象
     * @return 通用返回对象
     */
    @PostMapping("/queryIndicatorTrend")
	@ApiOperation(value = "查询网端指标趋势", notes = "查询网端指标趋势")
    public R queryIndicatorTrend(@RequestBody SegmentOverviewParams params) {
        return R.ok(segmentOverviewService.queryIndicatorTrend(params));
    }
    /**
     * 查询网元趋势
     *
     * @param params 网端总览查询对象
     * @return 通用返回对象
     */
    @PostMapping("/queryNetworkTrend")
	@ApiOperation(value = "查询网元趋势", notes = "查询网元趋势")
    public R queryNetworkTrend(@RequestBody SegmentOverviewParams params) {
        return R.ok(segmentOverviewService.queryNetworkTrend(params));
    }
	/**
	 * 修改网端坐标
	 *
	 * @param segmentPositionVo 网端坐标vo对象
	 * @return 通用返回对象
	 */
	@ApiImplicitParams({
			@ApiImplicitParam(name = "segmentId", value = "网端id", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "systemId", value = "系统id", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "locationY", value = "纵坐标", paramType = "body", required = true, dataType = "Double"),
			@ApiImplicitParam(name = "locationX", value = "横坐标", paramType = "body", required = true, dataType = "Double"),
	})
	@PostMapping("/updateSegmentPosition")
	public R updateSegmentPosition(@RequestBody SegmentPositionVo segmentPositionVo) {
		return segmentOverviewService.updateSegmentPosition(segmentPositionVo);
	}

    /**
     * 查询返回码统计信息
     *
     * @param params 查询返回码统计信息参数
     * @return 通用返回对象
     */
    @PostMapping("/queryRetStatisInfo")
	@ApiOperation(value = "查询返回码统计信息", notes = "查询返回码统计信息")
    public R queryRetStatisInfo(@RequestBody QueryRetStatisInfoParams params) {

        // 调用服务
        List<RetStatisInfoVo> rsiVoList = segmentOverviewService.queryRetStatisInfo(params);
        return R.ok(rsiVoList);
    }




    /**
     * 查询网端数据总览
     *
     * @param params 分页参数
     * @return 同一返回对象
     */
    @PostMapping("/querySegmentDataOverview")
	@ApiOperation(value = "查询网端数据总览", notes = "查询网端数据总览")
    public R querySegmentDataOverview(@RequestBody SegmentOverviewParams params) {
        if (params == null) {
            return R.failed("参数不能为空");
        }
        if (params.getSystemId() == null) {
            return R.failed("系统Id不能为空");
        }
        return R.ok(segmentOverviewService.querySegmentDataOverview(params));
    }


    @PostMapping("/querySegmentDataOverviewExport")
	@ApiOperation(value = "网端数据总览下载", notes = "网端数据总览下载")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "segmentIdList", 			value = "网端id", 			paramType = "query", 	required = false, 	dataType = "Array"),
			@ApiImplicitParam(name = "systemId", 			value = "系统id", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "orderByColumn", 			value = "排序字段", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "isAsc", 			value = "排序规则（DESC降序、ASC升序）", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "endTime", 			value = "结束时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "startTime", 			value = "结束时间微妙值", 			paramType = "query", 	required = false, 	dataType = "Long"),
			@ApiImplicitParam(name = "pageNum",   			value = "页码", 				paramType = "query", 	required = true, 	dataType = "Long"),
			@ApiImplicitParam(name = "pageSize", 			value = "每页大小", 			paramType = "query", 	required = true, 	dataType = "Long"),
	})
    public void querySegmentDataOverviewExport(@RequestBody SegmentOverviewParams params, HttpServletResponse response) throws IOException {
        List<SegmentOverviewVo> list = new ArrayList<>();
        params.setPageNum(1);
        params.setPageSize(1000000L);
        PageDataInfo pageDataInfo = BeanUtils.cast(querySegmentDataOverview(params).getData());
        list = BeanUtils.cast(pageDataInfo.getData());
        List<List<String>> headList = new ArrayList<>();

        String formTitleName = "网端总览列表-" + params.getSystemName() + "系统" + Constants.DOUBLE_SPACE  + params.getStartTime() + "~" + params.getEndTime();
        List<String> titleList = Arrays.asList("序号","网端名称", "请求总量", "失败请求", "告警请求", "健康度");
        titleList.forEach(s -> {
            List<String> childHeadList = new ArrayList<>();
            childHeadList.add(formTitleName);
            childHeadList.add(s);
            headList.add(childHeadList);
        });

        //        健康度添加 %
        int index = 1;
        for (SegmentOverviewVo segmentOverviewVo : list) {
            segmentOverviewVo.setNumber(index++);
            segmentOverviewVo.setHealthRate(segmentOverviewVo.getHealthRate() == null ? "0" : segmentOverviewVo.getHealthRate() + "%");
        }
        ExcelUtil.writeExcel(SegmentOverviewVo.class, "formTitleName", "网端总览列表", list, headList, response);

    }


}
